package com.example.demo08;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

public class BaseTextPage extends Fragment {
	TextView text;
	StringBuilder sb = new StringBuilder();
	HorizontalScrollView hscroll;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_text, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		text = (TextView) view.findViewById(R.id.text);
		hscroll = (HorizontalScrollView) view.findViewById(R.id.hscroll);
		
		onInit();
	}
	
	protected void setHScroll(boolean scroll) {
		hscroll.setScrollContainer(scroll);
//		hscroll.setScrollContainer(scroll);
		getView().requestLayout();
	}
	
	protected void onInit() {
		
	}

	final Runnable r = new Runnable() {

		@Override
		public void run() {
			text.setText(sb.toString());
		}
	};

	void update() {
		getActivity().runOnUiThread(r);
	}

	BaseTextPage append(String s) {
		sb.append(s);
		return this;
	}

	BaseTextPage appendn(String s) {
		sb.append(s).append('\n');
		return this;
	}

	void set(String s) {
		sb.delete(0, sb.length());
		sb.append(s);
	}

	BaseTextPage newLine() {
		sb.append('\n');
		return this;
	}

	void clear() {
		sb.delete(0, sb.length());
	}
	
	@Override
	public String toString() {
		return sb.toString();
	}
}
